package com.alibaba.citrus.cr.order.open.api.facade.convert;

import com.alibaba.cloud.retail.store.api.service.CargoZoneQueryService;
import com.alibaba.cloud.retail.store.request.cargozone.QueryCargoZoneRequest;
import com.alibaba.cloud.retail.store.response.ResultPager;
import com.alibaba.cz.base.model.store.CargoZoneSDO;
import com.alibaba.cz.base.tool.result.Result;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @description:
 * @author: yuhw
 * @date: 2024年01月09日 19:40
 **/
@Component
public class WarehouseConvert {

    @DubboReference
    CargoZoneQueryService cargoZoneQueryService;

    public Map<String, CargoZoneSDO> getCargoZoneSDOMap(List<String> idList){
        Map<String,CargoZoneSDO> map = Maps.newHashMap();
        if (CollectionUtils.isEmpty(idList)){
            return map;
        }
        QueryCargoZoneRequest queryCargoZoneRequest = new QueryCargoZoneRequest();
//        queryCargoZoneRequest.setIds(idList);
        queryCargoZoneRequest.setCargoZoneIds(idList);
        queryCargoZoneRequest.setSize(idList.size());
        Result<ResultPager<List<CargoZoneSDO>>> listResult = cargoZoneQueryService.searchCargoZoneList(queryCargoZoneRequest);
        Optional.ofNullable(listResult).map(Result::getResult).map(ResultPager::getContent).orElse(Lists.newArrayList())
                .stream().forEach(cargoZoneSDO -> map.put(cargoZoneSDO.getId(),cargoZoneSDO));
        return map;
    }

    public Map<String, CargoZoneSDO> getCargoZoneSDOMapByCodes(List<String> codes){
        Map<String,CargoZoneSDO> map = Maps.newHashMap();
        if (CollectionUtils.isEmpty(codes)){
            return map;
        }
        QueryCargoZoneRequest queryCargoZoneRequest = new QueryCargoZoneRequest();
//        queryCargoZoneRequest.setCargoZoneIds(idList);
        queryCargoZoneRequest.setCodes(codes);
        queryCargoZoneRequest.setSize(codes.size());
        queryCargoZoneRequest.setStart(0);
        Result<ResultPager<List<CargoZoneSDO>>> listResult = cargoZoneQueryService.searchCargoZoneListByDB(queryCargoZoneRequest);
        Optional.ofNullable(listResult).map(Result::getResult).map(ResultPager::getContent).orElse(Lists.newArrayList())
                .stream().forEach(cargoZoneSDO -> map.put(cargoZoneSDO.getCode(),cargoZoneSDO));
        return map;
    }

}
